package com.hyzx.qbasic.admin.controller.system;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.constant.PermissionConst;
import com.hyzx.qbasic.admin.controller.BaseController;
import com.hyzx.qbasic.admin.model.annotation.SysLog;
import com.hyzx.qbasic.admin.model.po.AdminUserInfoPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.*;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.model.vo.res.BaseResModel;
import com.hyzx.qbasic.admin.model.vo.res.admin.GetUserInfoRes;
import com.hyzx.qbasic.admin.common.util.BaseResUtils;
import com.hyzx.qbasic.admin.domain.service.IAdminUserService;
import com.hyzx.qbasic.admin.domain.service.IOrgService;
import com.hyzx.qbasic.admin.domain.service.IRoleService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by 黄江华 on 2017/9/29
 */

@Api(tags = "用户管理", description = "UserController")
@Controller
@RequestMapping("/sys/user")
public class UserController extends BaseController {

    @Autowired
    private IAdminUserService userService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IOrgService orgService;

    @SysLog("用户管理首页")
    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("addFunc", getFunction(PermissionConst.SYS_USER_ADD));
        model.addAttribute("editFunc", getFunction(PermissionConst.SYS_USER_EDIT));
        model.addAttribute("deleteFunc", getFunction(PermissionConst.SYS_USER_DELETE));
        model.addAttribute("resetPassFunc", getFunction(PermissionConst.SYS_USER_RESET_PASS));

        return "system/user/list";
    }

    @SysLog("获取用户列表")
    @GetMapping("/getUserList")
    @ResponseBody  // 返回对象（JSON），不加返回页面路径，默认template
    public BaseResModel<PageInfo<AdminUserInfoPO>> getUserList(@Valid GetUserListReq req, BindingResult result) {
        return BaseResUtils.success(userService.getPagedUserList(req));
    }

    @SysLog("新增用户页")
    @GetMapping("/add")
    public String add(Model model) {
        model.addAttribute("roleList", roleService.getAllRoleNameList());
        return "system/user/add";
    }

    @SysLog("新增用户表单提交")
    @PostMapping("/add")
    @ResponseBody
    public BaseResModel<BaseOperationRes> add(@Valid @RequestBody AddUserReq req, BindingResult result) {
        BaseOperationRes res = userService.addUser(req);
        return BaseResUtils.success(res);
    }

    @SysLog("删除用户")
    @PostMapping("/delete")
    @ResponseBody
    public BaseResModel<BaseOperationRes> delete(@Valid @RequestBody DeleteUserReq req, BindingResult result) {
        BaseOperationRes res = userService.deleteUser(req);
        return BaseResUtils.success(res);
    }

    @SysLog("编辑用户页")
    @GetMapping("/edit")
    public String edit(Model model) {
        model.addAttribute("roleList", roleService.getAllRoleNameList());
        return "system/user/edit";
    }

    @SysLog("编辑用户表单提交")
    @PostMapping("/edit")
    @ResponseBody
    public BaseResModel<BaseOperationRes> edit(@Valid @RequestBody EditUserReq req, BindingResult result) {
        BaseOperationRes res = userService.editUser(req);
        return BaseResUtils.success(res);
    }

    @SysLog("获取用户信息")
    @GetMapping("/getUserInfo")
    @ResponseBody
    public BaseResModel<GetUserInfoRes> getUserInfo(@Valid GetUserInfoReq req, BindingResult result) {
        GetUserInfoRes res = new GetUserInfoRes();
        res.setAdminUserInfo(userService.getUserInfo(req.getUserId()));
        res.setRoleList(roleService.getUserRoleNameList(req.getUserId()));
        res.setOrgId(orgService.getUserOrgId(req.getUserId()));

        return BaseResUtils.success(res);
    }

    @SysLog("重置密码页")
    @GetMapping("/resetPass")
    public String resetPass() {
        return "system/user/resetPass";
    }

    @SysLog("重置密码表单提交")
    @PostMapping("/resetPass")
    @ResponseBody
    public BaseResModel<BaseOperationRes> resetPass(@Valid @RequestBody ResetPassReq req, BindingResult result) {
        BaseOperationRes res = userService.resetPass(req);
        return BaseResUtils.success(res);
    }
}
